<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\ClassPlan as UM;
use app\admin\validate\ClassPlanValidate;
use think\Db;

class ClassPlan extends Common
{
    private $noCheckAction = [];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
        $this->doCheckAuth($this->noCheckAction);
    }
    /**
    * 管理员列表
    */
    public function index()
    {
        $model = new UM();
        $request = $this->request->get();
        $search_where = $model->_where($request);
        $where = $search_where['where'];
        $search = $search_where['search'];
        $data = $model->_list($where);
        $count = $model->_count($where);
        
        $this->assign([
            'search' => $search,
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
        ]);
        
        return $this->fetch();
    }

    /**
    * 添加
    */
    public function add(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $validate = new ClassPlanValidate();
            if(!$validate->scene($validate::SCENE_ADD)->check($request)){
                die(outputError($validate->getError()));
            }
            
            $model = new UM();
            
            $ret = $model->_create($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '添加成功'));
        }else{
            $request = $this->request->get();
            $this->assign([
                'class_id' => (int)$request['class_id']
            ]);
            return $this->fetch();
        }
    }
    
    /**
    * 编辑管理员
    */
    public function edit(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $validate = new ClassPlanValidate();
            if(!$validate->scene($validate::SCENE_EDIT)->check($request)){
                die(outputError($validate->getError()));
            }
            $model = new UM();

            //$ret = $model->_modify($request);
            $map['id'] = $request['id'];
            $up['sort'] = $request['sort'];
            $up['title'] = $request['title'];
            $ret = Db::name('class_plan')->where($map)->update($up);
            if(!$ret){
                die(outputError($model->getError()));
            }
            die(outputSuccess([], '编辑成功'));
        }else{
            $request = $this->request->get();

            $id = (int)$request['id'];
            $user_model = new UM();
            $data = $user_model->_findOne($id);
            $this->assign([
                'data'=>$data,
            ]);
            return $this->fetch();
        }
    }
    
    /**
    * 删除管理员
    */
    public function del(){
        if($this->request->isAjax()){
            $id = (int)$this->request->post('id');
            
            $validate = new ClassPlanValidate();
            if(!$validate->scene($validate::SCENE_REMOVE)->check(['id'=>$id])){
                die(outputError($validate->getError()));
            }
            
            $model = new UM();
            $ret = $model->_remove($id);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    /**
    * 批量删除
    */
    public function delAll(){
        if($this->request->isAjax()){
            $ids = (string)$this->request->post('ids');
            $model = new UM();
            $ret = $model->_removeAll($ids);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    
   
    
    /**
    * 设置管理状态
    */
    public function set_status(){
        $request = $this->request->post();
        //var_dump($request);die;
        $model = new UM();
        $ret = $model->_setStatus($request);
        if(!$ret){
            die(outputError($model->getError()));
        }
        
        die(outputSuccess());
    }
    
    
    /**
    * 查看设置详情
    */
    public function detail(){
        $id = (int)$this->request->get('id');
        
        $model = new UM();
        $info = $model->_findOne($id);
        $this->assign([
            'info' => $info
        ]);
        
        return $this->fetch();
    }
}
